//
//  LyMainViewController.swift
//  LySwift微博
//
//  Created by 贠明东 on 15/11/23.
//  Copyright © 2015年 itcCZ. All rights reserved.
//

import UIKit

class LyMainViewController: UITabBarController {

    ///发布按钮点击事件
    func composeClick()
    {
        //__FUNCTION__打印方法名称
        print(__FUNCTION__)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        //自定义的tabBar
//        let nweTabBar = LyTabBar()
//        //KVC
//        setValue(nweTabBar, forKey: "tabBar")
//        
//        //获取(添加composeButton点击事件)
//        nweTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //大招 Item全局颜色(PS:这里只能用在图标标和文字是一样的颜色才可以)
        tabBar.tintColor = UIColor.orangeColor()
        
        ///首页
        let homeVC = LyHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        ///消息
        let messageVC = LyMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        ///占位控制器(发布按钮)
        addChildViewController(UIViewController())
        
        ///发现
        let discoverVC = LyDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        ///我
        let profileVC = LyProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
        // 在这里添加撰写按钮太早了,会被占位item给挡住了
        //        // 设置撰写按钮的frame
        //        let width = tabBar.bounds.size.width / 5
        //        let frame = CGRect(x: 2 * width, y: 0, width: width, height: tabBar.bounds.height)
        //        composeButton.frame = frame
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置发布按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: width * 2 - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
    }
    
    ///从TabBarC抽出来的方法(自定义添加TabBar子控件)
    //private 表示只有本类或者当前文件的其他类可以访问
   private func addChildViewController(controller: UIViewController , title: String , imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //拼接高亮图片名字
        let highlightedImageName = imageName + "_highlighted"
        //让选中的图片使用原来的颜色，不要系统帮我们渲染(AlwaysOriginal:永远都用原来我们使用的颜色)
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
//        //小招：设置高亮图片
//        controller.tabBarItem.selectedImage = highlightedImage
//        
//        //小招：设置Item字体颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }

    ///懒加载
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置参数
        //设置发布按钮背景普通图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //设置发布按钮背景高亮图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置发布按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        //设置发布按钮高亮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到TabBar里面
        self.tabBar.addSubview(button)
        
        //获取(添加composeButton点击事件)
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()
}
